var express = require('express');
var router = express.Router();
var log4js = require('log4js');
var logger = log4js.getLogger();
var config = require('../../app/conf/version.json');

router.get("/", function(req, resp) {
    var params = req.query;
    logger.info("client get version: %s, platform : %s, deviceid : %s", params.version, params.platform, params.deviceid);
    if (!params) {
        logger.error("version query params empty");
        resp.write(JSON.stringify({resultCode : -100, msg : 'query params empty'}));
        resp.end();
    }
    if (!params.hasOwnProperty("version")) {
        logger.error("version query without version id");
        resp.write(JSON.stringify({resultCode : -101, msg : 'query without version id'}));
        resp.end();
    }
    if (!params.hasOwnProperty("platform")) {
        logger.error("version query without platform");
        resp.write(JSON.stringify({resultCode : -102, msg : 'query without platform'}));
        resp.end();
    }
    if (!params.hasOwnProperty("deviceid")) {
        logger.error("version query without deviceID");
        resp.write(JSON.stringify({resultCode : -103, msg : 'query without deviceid'}));
        resp.end();
    }
    logger.info("版本校验 version:" + params.version);
    if (config.testEvn.testDevices.indexOf(params.deviceid) !== -1) {
        // 测试设备
        var versionDetail = calcHistoryUpdates(config.testEvn.versions, params.version);
        // var versionDetail = config.testEvn.versions[params.version];
        if (!!versionDetail) {
            var wholeAppUpdateUrl = config.testEvn.wholeAppUpdateUrl[params.platform];
            logger.info("version : %s, need %s update", params.version, versionDetail.code);
            resp.write(JSON.stringify({resultCode : 0, versionDetail : JSON.stringify(versionDetail), url : wholeAppUpdateUrl, cdnUrl : config.testEvn.cdnUrl}));
            resp.end();
        } else {
            logger.error("version not in list");
            resp.write(JSON.stringify({resultCode : 404}));
            resp.end();
        }
    } else {
        var versionDetail = calcHistoryUpdates(config.productEvn.versions, params.version);
        // var versionDetail = config.productEvn.versions[params.version];
        if (!!versionDetail) {
            var wholeAppUpdateUrl = config.productEvn.wholeAppUpdateUrl[params.platform];
            logger.info("version : %s, need %s update", params.version, versionDetail.code);
            resp.write(JSON.stringify({resultCode : 0, versionDetail : JSON.stringify(versionDetail), url : wholeAppUpdateUrl, cdnUrl : config.productEvn.cdnUrl}));
            resp.end();
        } else {
            logger.error("version not in list");
            resp.write(JSON.stringify({resultCode : 404}));
            resp.end();
        }
    }
});

function calcHistoryUpdates(versions, version) {
    var versionDetail = versions[version];
    if (!versionDetail) {
        return null;
    }
    for (var key in versions) {
        var item = versions[key];
        if (isLower(version, key)) {
            versionDetail.systems.niuniu = versionDetail.systems.niuniu || item.systems.niuniu;
            versionDetail.systems.zhajinhua = versionDetail.systems.zhajinhua || item.systems.zhajinhua;
        }
    }
    return versionDetail;
};

function isLower(version1, version2) {
    if (!version1 || !version2) {
        return false;
    }
    var versions1 = version1.split('.');
    var versions2 = version2.split('.');
    var versionNum1 = parseInt(versions1[0]) * 1000000 + parseInt(versions1[1])*1000 + parseInt(versions1[2]);
    var versionNum2 = parseInt(versions2[0]) * 1000000 + parseInt(versions2[1])*1000 + parseInt(versions2[2]);
    return versionNum1 < versionNum2;
};

module.exports = router;
